﻿using System.Collections;
using UnityEngine;

namespace E.Tool
{
    public class BulletCreator : MonoBehaviour
    {
        public Bullet bullet;

        public int shootCount = 1;
        public float shootInterval = 0.2f;

        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (shootCount == 1)
                {
                    CreateBullet();
                }
                else
                {
                    CreateBullets();
                }
            }
        }
        private void OnDrawGizmos()
        { 
            Vector3 vec = transform.position;
            Gizmos.DrawLine(vec + Vector3.left / 2, vec + Vector3.right / 2);
            Gizmos.DrawLine(vec + Vector3.up / 2, vec + Vector3.down / 2);
        }


        public Bullet CreateBullet()
        {
            GameObject go = Instantiate(bullet.gameObject, transform.transform.position, transform.rotation);
            Bullet bu = go.GetComponent<Bullet>();
            bu.Init();
            return bu;
        }

        public void CreateBullets()
        {
            StartCoroutine(ICreateBullet());
        }

        private IEnumerator ICreateBullet()
        {
            for (int i = 0; i < shootCount; i++)
            {
                CreateBullet();
                yield return new WaitForSeconds(shootInterval);
            }
        }
    }
}